package com.restkeeper.controller;

import com.alibaba.fastjson.JSONObject;
import com.restkeeper.config.WeChatConfig;
import com.restkeeper.entity.SystemConfig;
import com.restkeeper.mapper.SystemConfigMapper;
import com.restkeeper.service.CommonService;
import com.restkeeper.service.SystemConfigService;
import com.restkeeper.utils.Result;
import com.restkeeper.utils.Snowflake;
import com.restkeeper.utils.StringUtils;
import com.restkeeper.utils.http.HttpUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


/**
 * 配置相关
 * */
@RestController
@RequestMapping("/systemConfig")
@RequiredArgsConstructor
public class SystemConfigController {

    private final SystemConfigService systemConfigService;
    private final SystemConfigMapper systemConfigMapper;
    private final CommonService commonService;
    private final WeChatConfig weChatConfig;



//    /**
//     * 获取邀请海报链接
//     * */
//    @GetMapping("/getInviteQrCodeLink")
//    public Result getInviteQrCodeLink(Long userId){
//        return systemConfigService.getInviteQrCodeLink(userId);
//    }

    /**
     * 获取app下载链接
     * */
    @GetMapping("/getAppDownloadLink")
    public Result getAppDownloadLink(){
        return systemConfigService.getAppDownloadLink();
    }

    /**
     * 版本控制
     * */
    @GetMapping("/versionInfo")
    public Result versionInfo(){
        return systemConfigService.versionInfo();
    }

    /**
     * 获取一个唯一id
     * */
    @GetMapping("/getSnowId")
    public Result getSnowId(){
        Snowflake snowflake = new Snowflake(1, 1);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("snowId", snowflake.nextId());
        return Result.ok(jsonObject);
    }

    /**
     * 获取一个测试设备
     * */
    @GetMapping("/getTempDevice")
    public Result getTempDevice(){
        SystemConfig systemConfig = systemConfigMapper.selectOneByKey("tempDevice");
        return Result.ok(systemConfig);
    }

    /**
     * 获取一个测试设备
     * */
    @GetMapping("/title")
    public Result title(){
        JSONObject json=new JSONObject();
        json.put("remainingCount","剩余");
        json.put("remainingCount1","剩余次数");
        json.put("remainingCount_times","次");
        json.put("usedCount","已用次数：");
        json.put("handleDeviceStart","启动设备");
        json.put("startDevice_showToast","启动成功213");
        json.put("no_times","次数已用完啦啦啦 ");
        return Result.ok(json);
    }

    /**
     * 视频开关
     * */
    @GetMapping("/getVideoFlag")
    public Result getVideoFlag(){
        return systemConfigService.getVideoFlag();
    }



    /**
     *短剧解锁集数
     */
    @GetMapping(value = "shortUnlock")
    public Result shortUnlock()
    {
        return systemConfigService.shortUnlock();
    }

    public static String gettoken() {

        WeChatConfig weChatConfig=new WeChatConfig();
        weChatConfig.setAppId("wx32ba92c86d0d7306");
        weChatConfig.setAppSecret("a8d90c623646eb917033d24f9677b352");
        String s = HttpUtils.sendGet(weChatConfig.ACCESS_TOKEN_URL.replace("APPID",weChatConfig.getAppId()).replace("SECRET",weChatConfig.getAppSecret()));
        if(StringUtils.isNoneBlank(s)){
            JSONObject jsonObject=JSONObject.parseObject(s);
            String accessToken = jsonObject.getString("access_token");
            System.out.printf("accessToken ={}",accessToken);
            return accessToken;
//            String res = HttpUtils.sendPost("https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+accessToken,msg);
//            System.out.printf("s={}",res);
        }
        return "";
    }

    public static void main1(String[] args) throws UnsupportedEncodingException {
//        String originalString = "https://wzzwl.online/device/weChat/oauth";
//        String originalString = "http://douduo.wfzwl.cn";
        String originalString = "http://web.hfxxzh.cn";
        String originalString2 = "http://douduo.wfzwl.cn/api/weChat/scancode";
        String originalString3 = "https://bj.xjdkj.cn?routerType=deviceInfo";
        String originalString4 = "http://bj.xjdkj.cn/device?routerType=home";
        String encodedString = URLEncoder.encode(originalString, "UTF-8");
        String encodedString2 = URLEncoder.encode(originalString2, "UTF-8");
        String encodedString3 = URLEncoder.encode(originalString3, "UTF-8");
        String encodedString4 = URLEncoder.encode(originalString4, "UTF-8");
        System.out.println("Encoded: " + encodedString);
        System.out.println("encodedString2: " + encodedString2);
        System.out.println("originalString3: " + encodedString3);
        System.out.println("encodedString4: " + encodedString4);
    }
    public static void main(String[] args) {

        try {
            String msg="{\n" +
                    "    \"button\": [\n" +

                    "        {\n" +
                    "            \"type\": \"view\", \n" +
                    "            \"name\": \"我的设备\", \n" +
                    "            \"key\": \"llk-view-device\", \n" +
                    "            \"url\": \"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx32ba92c86d0d7306&redirect_uri=http%3A%2F%2Fweb.hfxxzh.cn%3FrouterType%3Dhome&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect\"\n" +
                    "        }\n" +
                    "    ]\n" +
                    "}";
            String accessToken =gettoken();
            String res = HttpUtils.sendPost("https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+accessToken,msg);
            System.out.printf("s={}",res);
        }catch (Exception e){
            e.printStackTrace();
        }

    }


}
